package uranium.gui.view;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import uranium.db.DBManager;
import uranium.db.model.TableModelGenerics;
import javax.swing.JTextField;

import org.jdesktop.swingx.JXTable;

public class ViewFattureGUI extends JDialog{

	private JFrame jFrame = null;  //  @jve:decl-index=0:visual-constraint="42,26"
	private JPanel jContentPane = null;
	private JButton jButton = null;
	private JButton jButton1 = null;
	private JScrollPane jScrollPane = null;
	private JXTable jTable1 = null;
	private DBManager dbm = null;
	private JLabel jLabel = null;
	private TableModelGenerics model = null;
	private JTextField jTextField1 = null;
	private JButton jButton2 = null;
	public ViewFattureGUI(DBManager dbm, JFrame jFrame){
		super(jFrame, true);
		this.jFrame = jFrame;
		this.dbm = dbm;
		initialize();
	}
	
	private void initialize(){
		//jFrame = new JFrame();
		this.setSize(new Dimension(639, 468));
		this.setTitle("Visualizza Fatture");
		this.setContentPane(getJContentPane());
		centro();
		this.setVisible(true);
	}
	
	private void centro()
	{
		int x = (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() - this.getWidth()) / 2;
		int y = (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight() - this.getHeight()) / 2;
		this.setLocation(x, y);
	}

	/**
	 * This method initializes jContentPane	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(28, 43, 133, 16));
			jLabel.setText("Cerca per numero");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getJButton(), null);
			jContentPane.add(getJButton1(), null);
			jContentPane.add(getJScrollPane(), null);
			jContentPane.add(jLabel, null);
			jContentPane.add(getJTextField1(), null);
			jContentPane.add(getJButton2(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setBounds(new Rectangle(192, 63, 90, 30));
			jButton.setText("Cerca");
			jButton.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					try{
						if (jTextField1.getText().trim().equals("")){
							model = new TableModelGenerics(dbm,"","","fatture");
							jTable1.setModel(model);
						}
						else {
							model = new TableModelGenerics(dbm,"numero",jTextField1.getText().trim(),"fatture");
							jTable1.setModel(model);
						}
					}
					catch(Exception e29){
						e29.printStackTrace();
					}
				}
			});
		}
		return jButton;
	}

	/**
	 * This method initializes jButton1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setBounds(new Rectangle(140, 390, 90, 30));
			jButton1.setText("OK");
			jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					try{
						close();
					}
					catch(Exception e28){
						System.err.println(e28);
					}
				}
			});
		}
		return jButton1;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(new Rectangle(15, 120, 601, 241));
			jScrollPane.setViewportView(getJTable1());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jTable1	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getJTable1() {
		if (jTable1 == null) {
			jTable1 = new JXTable();
			model = new TableModelGenerics(dbm,"","","fatture");
			jTable1.setModel(model);
			jTable1.setRowSelectionAllowed(true);
			jTable1.getCellEditor();
			jTable1.setColumnControlVisible(true);
		}
		return jTable1;
	}

	/**
	 * This method initializes jTextField1	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField1() {
		if (jTextField1 == null) {
			jTextField1 = new JTextField();
			jTextField1.setBounds(new Rectangle(29, 71, 109, 20));
		}
		return jTextField1;
	}
	
	private void close(){
		this.dispose();
	}

	/**
	 * This method initializes jButton2	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton2() {
		if (jButton2 == null) {
			jButton2 = new JButton();
			jButton2.setBounds(new Rectangle(380, 390, 90, 30));
			jButton2.setText("Cancella");
			jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					int scelta = JOptionPane.showConfirmDialog(jFrame, "Sei sicuro?", "Conferma eliminazione", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
					if (scelta==0){
						int i = rimuovi();
						if( i == 0)
							JOptionPane.showMessageDialog(jFrame, "Rimozione non riuscita.", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
						else
							model.stateChange();
					}				
				}
			});
		}
		return jButton2;
	}
	
	private synchronized int rimuovi(){
		try{
			int row = jTable1.getSelectedRow();
			if (row < 0) 
				JOptionPane.showMessageDialog(jFrame, "Selezionare una riga", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			else{
				String codice = ((String)jTable1.getValueAt(row, 0));
				String query = "DELETE FROM fatture WHERE numero='"+codice+"'";
				dbm.executeQuery(query);
				return 1;
				//JOptionPane.showMessageDialog(jFrame, "Fattura eliminata", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			}
		}
		catch(Exception er){
			er.printStackTrace();
		}
		return 0;
	}
}
